package com.shang.core;

import com.shang.core.base.FixedMeta;
import com.shang.core.base.PadPosition;
import com.shang.core.base.Value;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 固定长度的BitMap，valueLength代表字符数
 * <p>
 * Created by shangwei2009@hotmail.com on 2021/9/8 14:21
 */
public class DeprecatedBitmap extends Value {
    public DeprecatedBitmap(FixedMeta meta) {
        super(meta, StandardCharsets.US_ASCII, PadPosition.NONE, "");
    }

    @Override
    protected String valueStr(String valueStr) {
        final byte[] bytes = DatatypeConverter.parseHexBinary(valueStr);
        return Arrays.stream(ArrayUtils.toObject(bytes))
                .map(aByte -> StringUtils.leftPad(Integer.toBinaryString(aByte & 0xFF), 8, '0'))
                .collect(Collectors.joining());
    }
}
